const Promise = require('promise');
const fp = require('lodash/fp');
const { Maybe, Container } = require('./support');
const myPromise = require('./myPromise');

const xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do']);

let safeProp = fp.curry(function(x, o) {
  return Maybe.of(o[x])
});

let user = {id: 2, name: 'Albert'};

let maybe = Maybe.of([5,6,1]);

let ex1 = maybe.map(x => fp.map(fp.add(1),x));

console.log('ex1',ex1);

let ex2 = xs.map(x => fp.first(x));

console.log('ex2', ex2);

let ex3 = fp.flowRight(fp.first, fp.map(fp.first),safeProp('name'));

console.log('ex3', ex3(user))

/**
 * 重构ex4
 * 
*/

let ex4 = function(n) {
  if(n) return parseInt(n);
}

const ex5 = n => {
  let ex4Maybe = Maybe.of(n);
  return ex4Maybe.map(x => parseInt(x));
}

let ex4Rew = fp.flowRight(fp.get('_value'),ex5)

console.log('ex4', ex4(2.5))
console.log('ex4Rew', ex4Rew(2.5))

/**
 * myPromise
*/

const p1 = new myPromise((resolve, reject) => {
  resolve('成功');
  // reject('失败');
})

p1.then(data => {
  console.log('p1',data)
})
